<?php
namespace Common\Controller;
use Think\Controller;

class PayController extends Controller{
//下单成功之后的相关逻辑


  public function paysuccess( $orderid,$userid)
    {   
      
       $jifen_xf_one= M('jifen')->getField('jifen_xf_one');

       $orderinfo= M('order')->find($orderid);
        $data['id']=$orderid;
        $data['paystatus']=1;
        $data['audit_time']=time();
        
         M()->startTrans();
         $res1=M('order')->save($data);

         $add_xfjifen=$orderinfo['moshi_total_xf'];
         $res2=M('user')->where("id=$userid")->setInc("xf_jifen",$add_xfjifen);
         $res3=M('user')->where("id=$userid")->setInc("ordernums",$orderinfo['order_num']);
         $record['user_id']=$userid;
         $record['items']='下单奖励积分';
         $record['jifen_num']='+'.$add_xfjifen;
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
        $status=M('user')->where("id=$userid")->getField('status');
         $map['user_id']=$userid;
         $map['xf_status']=0;
         $map['paystatus']=1;
         $orderxf_num=D('order')->getTotalNums($map); 
           //自身消费满10单，送上级积分
            //自身消费满10单，送上级积分 
        if ($orderxf_num>=10) {
            //总分的工资订单数
         $yfh_gz=floor($orderxf_num/10);
         $c['user_id']=$invite_id;
         $c['items']='您邀请的好友超10单奖励积分';
         $c['jifen_type']='xf';
  
            $fh_jf=M('jifen_record')->where($c)->count();
exit('ddfsdfsda');
            $n=$yfh_gz-$fh_jf;

          for ($i=1; $i <=$n ; $i++) { 
         $record['user_id']=$invite_id;
         $record['items']='您邀请的好友超10单奖励积分';
         $record['jifen_num']='+'.$orderinfo['jifen_xf_ten'];
         $record['addtime']=time();
         $record['jifen_type']='xf';
         M('jifen_record')->add($record);
          M('user')->where("id=$invite_id")->setInc("xf_jifen",$orderinfo['jifen_xf_ten']); 
          }
         
        }

  

        //获取上级id 自身团队
        $invite_id=M('user')->where("id=$userid")->getField('invite_id');
        if ($invite_id>0) {
          D('Order')->getOrderNums_fl($userid,$orderinfo['order_num']);
             //是否达到自动解绑   
          $iteration =D('Order')->getOrderNums_up($userid,$orderinfo['order_num']); 
            $invite_status=M('user')->where("id=$invite_id")->getField('status');
             if ($status>=$invite_status  ) {
                 M('user')->where("id=$userid")->setField('f_id',0);
                  $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$invite_id;
                          $beyond['beyond_time']=time();
                      M('user')->where("id=$userid")->save($beyond);
            }
          }
       
      

          $jb=M('jbset')->find();

          switch ($status) {
              case '1':
                  $months=$jb['sj_jb2_mons']-1;
                  break;
            case '2':
                  $months=$jb['sj_jb3_mons']-1;
                  break;
            case '3':
                   $months=$jb['sj_jb4_mons']-1;
                  break;
              
              default:
                  # code...
                  break;
          }

        $end=time();
         $begin=mktime(0,0,0,date('m')-$months,1,date('Y'));

        $getOrderNums_team =D('Order')->getTeamTotalNumsUpCum($userid,$begin,$end);
     

       /* }*/
        //消费者以上自身消费满10单
       if ($status>1) {
        unset($map);
        //自身消费满10单的条件
        $map['user_id']=$userid;


        $map['paystatus']=1;
   
        $self_ordernum=D('order')->getTotalNums($map);
        $team_ordernum=D('order')->getTeamTotalNumsNow($userid);

        


      if ($self_ordernum>=10) {

              $yfh_gz=floor($self_ordernum/10);
            $map['user_id']=$userid;

            $gz['user_id']=$userid;
            $gz['desc']='自身消费满10单';
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;
           
            for ($i=1; $i <=$n ; $i++) { 

                  $curtime=time();
            //D('order')->where($map)->setField('self_gz_status',1);
               unset($data);
               $jifen=M('jifen')->find();
                $data['user_id']=$userid;
                $data['addtime']=$orderinfo['audit_time'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];
                $data['desc']='自身消费满10单';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                    $a = date("w",$data['addtime']);
if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['addtime']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
}
     $data['begin_hf_time'] =strtotime($d)+604800; 

                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$orderid)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }




            }


        

        }
              
    }


         if ($res2 && $res1 && $res3) {
            M()->commit(); 
            return true;
         }else{
           M()->rollback();
           return false;
         }

    }


    
}